package com.sls.web.manager.recharge.service.impl;

import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.SupplierTmp;
import com.sls.web.manager.recharge.querybean.SupplierTmpQuery;
import com.sls.web.manager.recharge.service.SupplierTmpService;
import com.sls.web.manager.socket.SuperService;

@Service("supplierTmpService")
public class SupplierTmpServiceImpl extends SuperService implements
		SupplierTmpService {

	@Override
	public Pagination<?> querySupplierTmp(SupplierTmpQuery rQuery,
			UserInfoVO userinfo) throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPPLIER_TMP_LIST);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO socketDTO = super.send(head, rQuery);
		String body = socketDTO.getBody();
		Pagination<SupplierTmp> supplierTmp = gson.fromJson(body,
				new TypeToken<Pagination<SupplierTmp>>() {
				}.getType());

		return supplierTmp;
	}

	@Override
	public boolean delete(String id, UserInfoVO userinfo) throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DELETE_SUPPLIER_TMP);
		SupplierTmp st = new SupplierTmp();
		st.setId(id);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, st);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public boolean synchronous(String id, UserInfoVO userinfo) throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SYNCHRONOUS_SUPPLYIER);
		SupplierTmp st = new SupplierTmp();
		st.setId(id);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, st);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}
}
